﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RenShiKaoQin.Account
{
    public partial class RestRequest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            hrdbEntities ent = new hrdbEntities();
            /*
            var result = from emp in ent.TEmployees join dept in ent.TDepts on emp.FEmployeeID equals dept.FEmployeeID into empdepts from empdept in empdepts.DefaultIfEmpty() select new { emp.FName, empdept.FDeptCode };
             */
            var result = from emp in ent.TEmployees where emp.FEmployeeID.Equals(Page.User.Identity.Name) select emp;
            TEmployee employee = result.Single();
            TDept dept = employee.TDepts.OrderBy(p => p.FValidDate).Last();
            lbName.Text = employee.FName;
            var rdept = from tdept in ent.TDictDepts where tdept.FDeptCode.Equals(dept.FKQDeptCode) select tdept;
            try
            {
                lbDept.Text = rdept.Single().FName;
                lbDept.ID = rdept.Single().FDeptCode;
                var rindept = from tindept in ent.TDictDepts where tindept.FDeptCode.Equals(dept.FDeptCode) select tindept;
                lbInDept.Text = rindept.SingleOrDefault().FName;
            }
            catch
            {
                Response.Write("您还没有所属科室或者考勤科室，请联系人事处");
                Response.End();
            }
            lbGender.Text = employee.TDictGender != null ? employee.TDictGender.FName : "未知";
            if (employee.THeadShips.Any())
            {
                THeadShip headShip = employee.THeadShips.OrderBy(p => p.FValidDate).Last();
                lbHeadShip.Text = headShip.TDictHeadShip.FName;
            }
            else
            {
                lbHeadShip.Text = "无";
            }

            if (employee.TTitles.Any())
            {
                TTitle title = employee.TTitles.OrderBy(p => p.FValidDate).Last();
                lbZhiChen.Text = title.TDictTitle.FName;
            }
            else
            {
                lbZhiChen.Text = "无";
            }

            if (employee.TEngageJobs.Any())
            {
                TEngageJob job = employee.TEngageJobs.OrderBy(p => p.FValidDate).Last();
                lbGangWei.Text = job.TDictTitle.FName;
            }
            else
            {
                lbGangWei.Text = "无";
            }
            lbTel.Text = employee.FMobileTel;

            lbBirthday.Text = employee.FBirthday.HasValue? String.Format("{0:D}", employee.FBirthday.Value):"";

            ent.Dispose();
            sqlRestRecord.SelectParameters.Clear();
            sqlRestRecord.SelectParameters.Add("FEmployeeID", Page.User.Identity.Name);

            tbStart.Attributes["readonly"] = "readonly";
            tbEnd.Attributes["readonly"] = "readonly";
        }

        protected void btnApply_Click(object sender, EventArgs e)
        {
            if (tbStart.Text == "" || tbEnd.Text == "") return;
            using (hrdbEntities ent = new hrdbEntities())
            {
                DateTime st = Convert.ToDateTime(tbStart.Text);
                DateTime end = Convert.ToDateTime(tbEnd.Text);
                var result = from rec in ent.TKQRequestRecords where rec.StartDate.Equals(st) && rec.EndDate.Equals(end) && rec.FEmployeeID.Equals(User.Identity.Name) select rec;
                if (result.Any())
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "Cannotrepeat", "alert('您不能提交重复时间段的申请，如您想修改申请，请先撤销，然后提交新的');", true);
                    return;
                }
                TKQRequestRecord record = TKQRequestRecord.CreateTKQRequestRecord(Page.User.Identity.Name, Convert.ToDateTime(tbStart.Text), Convert.ToDateTime(tbEnd.Text), ddlType.SelectedItem.Value, false, false, 0, false, lbDept.ID, false);
                record.Memo = tbMemo.Text;
                ent.TKQRequestRecords.AddObject(record);
                ent.SaveChanges();
                PrintTable(record);
            }
            gvRequest.DataBind();
        }

        protected void gvRequest_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Print"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                string tt = gvRequest.DataKeys[index].Value.ToString();
                int serial = Convert.ToInt32(tt);
                BuildPrintTable(serial);
            }
        }
        protected void PrintTable(TKQRequestRecord rec)
        {
            TableCell cell11 = new TableCell();
            cell11.Text = "姓名";
            TableCell cell12 = new TableCell();
            cell12.Text = lbName.Text;
            TableCell cell13 = new TableCell();
            cell13.Text = "科室";
            TableCell cell14 = new TableCell();
            cell14.Text = rec.TDictDept.FName;
            TableCell cell15 = new TableCell();
            cell15.Text = "事由";
            TableCell cell16 = new TableCell();
            cell16.Text = rec.Memo;
            TableRow row1 = new TableRow();
            row1.Cells.Add(cell11); row1.Cells.Add(cell12); row1.Cells.Add(cell13); row1.Cells.Add(cell14); row1.Cells.Add(cell15); row1.Cells.Add(cell16);
            tableHolder.Rows.Add(row1);
            TableCell cell21 = new TableCell();
            cell21.ColumnSpan = 2;
            cell21.Text = "休假类别及时间";
            TableCell cell22 = new TableCell();
            cell22.ColumnSpan = 2;
            cell22.Text = rec.TDictKQType.FName;
            TableCell cell23 = new TableCell();
            cell23.ColumnSpan = 2;
            cell23.Text = String.Format("{0:D}", rec.StartDate) + "至" + rec.EndDate.ToString("D");
            TableRow row2 = new TableRow();
            row2.Cells.Add(cell21); row2.Cells.Add(cell22); row2.Cells.Add(cell23);
            tableHolder.Rows.Add(row2);
            TableCell cell31 = new TableCell();
            cell31.ColumnSpan = 2;
            cell31.Text = "科室意见";
            TableCell cell32 = new TableCell();
            cell32.ColumnSpan = 4;
            cell32.Text = "意见：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;签名：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;日期：";
            TableRow row3 = new TableRow();
            row3.Cells.Add(cell31); row3.Cells.Add(cell32);
            tableHolder.Rows.Add(row3);
            TableCell cell41 = new TableCell();
            cell41.RowSpan = 2;
            cell41.ColumnSpan = 2;
            cell41.Text = "审批部门意见";
            TableCell cell42 = new TableCell();
            cell42.ColumnSpan = 4;
            cell42.Text = "意见：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;签名：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;日期：";
            TableCell cell52 = new TableCell();
            cell52.ColumnSpan = 4;
            cell52.Text = "意见：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;签名：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;日期：";
            TableRow row4 = new TableRow();
            row4.Cells.Add(cell41); row4.Cells.Add(cell42);
            tableHolder.Rows.Add(row4);
            TableRow row5 = new TableRow();
            row5.Cells.Add(cell52);
            tableHolder.Rows.Add(row5);
            TableCell cell61 = new TableCell();
            cell61.ColumnSpan = 2;
            cell61.Text = "分管院领导意见";
            TableCell cell62 = new TableCell();
            cell62.ColumnSpan = 4;
            cell62.Text = "意见：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;签名：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;日期：";
            TableRow row6 = new TableRow();
            row6.Cells.Add(cell61); row6.Cells.Add(cell62);
            tableHolder.Rows.Add(row6);
            tableHolder.CellPadding = 10;
            tableHolder.Width = 670;
            tableHolder.Height = 350;
            ClientScript.RegisterStartupScript(this.GetType(), "PrintRequest", "$('#PrintArea').printArea();", true);
        }
        protected void BuildPrintTable(int serial)
        {
            using (hrdbEntities ent = new hrdbEntities())
            {
                var result = from kqrec in ent.TKQRequestRecords where kqrec.SerialNO == serial select kqrec;
                if (result.Any())
                {
                    TKQRequestRecord rec = result.Single();
                    //TimeSpan span = rec.EndDate - rec.StartDate;
                    //if (span.TotalHours <= 24)
                    //{
                    //    ClientScript.RegisterStartupScript(this.GetType(), "neednotprint", "alert('请假时间两天以内无需打印');", true);
                    //}
                    PrintTable(rec);
                }
            }

        }

        protected void gvRequest_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int serial = Convert.ToInt32(e.Keys[0]);
            using (hrdbEntities ent = new hrdbEntities())
            {
                var result = from rec in ent.TKQRequestRecords where rec.SerialNO == serial select rec;
                if (result.Any())
                {
                    TKQRequestRecord record = result.Single();
                    if (record.CheckStatus == false && record.ExamStatus == false)
                    {
                        e.Cancel = false;
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "Cannotdelete", "alert('只有未被审批和复核的申请才可以撤销');", true);
                        e.Cancel = true;
                    }
                }
            }
        }
    }
}